package com.mall.user.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mall.user.model.Address;
import com.mall.common.util.RespResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.mall.user.service.IAddressService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


/**
 * <p>
 * 收件信息表 控制器
 * </p>
 *
 * @author Darren
 * @since 2022-08-29
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "REST - 收件信息表")
@RestController
@RequestMapping("/address")
@CrossOrigin
public class AddressController {
    private final IAddressService service;

    @PostMapping("/addAddress")
    @ApiOperation(value = "新增")
    public RespResult addAddress(@RequestBody Address address){
        boolean flag = service.addAddress(address);
        return RespResult.ok(flag ? "创建成功" : "创建失败");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除")
    public RespResult deleteById(@PathVariable("id") Integer id){
        boolean flag = service.deleteById(id);
        return RespResult.ok(flag ? "删除成功" : "删除失败");
    }

    @PutMapping("/updateAddress")
    @ApiOperation(value = "更新")
    public RespResult updateAddress(@RequestBody Address address){
        boolean flag = service.updateAddress(address);
        return RespResult.ok(flag ? "更新成功" : "更新失败");
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public RespResult getById(@PathVariable("id") Integer id){
        return RespResult.ok(service.getById(id));
    }

    @GetMapping("/selectList")
    @ApiOperation(value = "根据条件查询全部数据")
    public RespResult<Address> selectList(Address address){
        return RespResult.ok(service.selectList(address));
    }

    @GetMapping("/selectPage")
    @ApiOperation(value = "分页查询")
    public RespResult selectPage(Page<Address> page, Address address){
        return RespResult.ok(service.selectPage(page, address));
    }

    @GetMapping("/list")
    @ApiOperation(value = "根据用户名称获取全部地址")
    public RespResult<Address> list(){
        String userName = "gp";
        Address address = new Address();
        address.setUsername(userName);
        return RespResult.ok(service.selectList(address));
    }
}